<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
	
	public function __construct(){
		parent::__construct();
		
		if(empty($_SESSION['user']))
		{
			 $this->error('对不起,您还没有登录!请先登录!', U('Login/login'), 1);
			//$this->redirect("Login/login"); 
			/*echo "<script>alert('对不起,您还没有登录!请先登录!');top.location.href='index.php/Home/Login/login.html';</script>";
			*/
		}
	}
	
    public function index(){
	  	$name = $_SESSION['user'];
		$account = M("account")->where(array('username'=>$name))->find();
		
		$this->assign('account',$account);
		$this->display();
    }
	
	//财务订单明细
	public function order_details(){
		$where['id']=$_REQUEST['id'];
		$order = M("order");
		$number = $order->field("order_number")->where($where)->find();
		$order_number = $number["order_number"];
		
		$order_details = M('order_info')->where(array('order_number'=>$order_number,'sales_return'=>''))->select();
		$acc_type = M('account')->field('look_details')->where(array('username'=>$_SESSION['user']))->find();
		$this->assign('acc_type',$acc_type);	
		$this->assign("order_details",$order_details);
		$this->display();
	}
	
	
	//财务管理
	public function financial_management(){
		$salesman_list = M('order')->field('salesman')->group('salesman')->select();
		
		$pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		
		$search_number	=	I('get.search_number','','htmlspecialchars');
		$is_finish	=	I('get.is_finish','','htmlspecialchars');
		$salesman 	= I('get.salesman','','htmlspecialchars');
		$delivery_time_start		=	I('get.delivery_time_start','','htmlspecialchars');
		$delivery_time_end		=	I('get.delivery_time_end','','htmlspecialchars');
		$customer_name	=	I('get.customer_name','','htmlspecialchars');
		$tracking_number = I('get.tracking_number','','htmlspecialchars');
		$wheres['order_number'] = $search_number;
		$wheres['delivery_time_start'] = $delivery_time_start;
		$wheres['delivery_time_end'] = $delivery_time_end;
		$wheres	['customer_name'] = $customer_name;
		$wheres['is_finish'] = $is_finish;
		$wheres['salesman'] = $salesman;
		$wheres['tracking_number'] = $tracking_number;
		
		$index = D("index");
		$res = $index->financial_list($pageNo,$pageSize,$wheres);
		$order_list = $res['order_list'];
		
		
		$where['id']			=	empty($_REQUEST['id'])?1:trim($_REQUEST['id']);
		$exchange_rate = M("exchange_rate")->where($where)->find();
		
		foreach($order_list as $key=>$val){
			$arrs = '';

			//相同订单号下的 total_price 的 总和
			$total = M('order_info')->where(array('order_number'=>$val['order_number'],'stauts'=>'4','sales_return'=>''))->SUM('total_price');
			//相同订单号下的 total_cost 的 总和
			$cost = M('order_info')->where(array('order_number'=>$val['order_number'],'stauts'=>'4','sales_return'=>''))->SUM('total_cost');
			$order_list[$key]['total_price'] = $total*$val['discount_rate']+$val['feright']; 
			$order_list[$key]['total_cost'] = $cost; 
			$order_list[$key]['discount_price'] = $total*$val['discount_rate'];
			
			$gross_profit = ($total*$val['discount_rate']+$val['feright'])*$exchange_rate['usd']-$val['actual_feright']-$cost;
			$order_list[$key]['gross_profit'] = $gross_profit;
			$order_list[$key]['gross_profit_margin'] = $gross_profit/(($total*$val['discount_rate']+$val['feright'])*$exchange_rate['usd']);
			
			$arr=explode('.',$order_list[$key]['gross_profit_margin']);
			
			$num1=substr($arr[1],0,2);
			$str = $arr[0].'.'.$num1;
			$order_list[$key]['percentage'] = ($str*100).'%';
		
		
		}
		
		$count['count']=$res['count']['count'];
		$count['countpage']=ceil($count['count']/$pageSize);
		$count['pageSize']=$pageSize;
		$count['pageNo']=$pageNo;
		$count['num']=$count['countpage']-4;
		$this->assign('salesman_list',$salesman_list);
		$this->assign('count',$count);
		
		
		
		$this->assign('search_number',$search_number);
		$this->assign('is_finish',$is_finish);
		$this->assign("salesman",$salesman);
		$this->assign("delivery_time_start",$delivery_time_start);
		$this->assign("delivery_time_end",$delivery_time_end);
		$this->assign("customer_name",$customer_name);
		$this->assign("tracking_number",$tracking_number);
		
		$this->assign('pageNo',$pageNo);
		$this->assign('pageSize',$pageSize);
		$this->assign("order_number",$wheres['order_number']);
		$this->assign("salesman_selected",$wheres['salesman']);
		$this->assign("is_finish",$wheres['is_finish']);
		$this->assign("delivery_time",$wheres['delivery_time']);
		$this->assign("customer_name",$wheres['customer_name']);
		$this->assign('type',$type);
		$this->assign("order_list",$order_list);
		$this->display();
	}
	
	
	//导出数据信息
	public function daochu_excel(){
		$ids = I('get.tids','','htmlspecialchars');
		$id_list = explode(',',trim($ids,','));
		foreach($id_list as $k=>$v){
			$order_list[$k] = M('order')->where(array('id'=>$v))->find();
			
			
			
			$where['id']			=	empty($_REQUEST['id'])?1:trim($_REQUEST['id']);
			$exchange_rate = M("exchange_rate")->where($where)->find();
			
			
			foreach($order_list as $key=>$val){$arrs = '';
				$total = M('order_info')->where(array('order_number'=>$val['order_number'],'stauts'=>'4','sales_return'=>''))->SUM('total_price');
				$cost = M('order_info')->where(array('order_number'=>$val['order_number'],'stauts'=>'4','sales_return'=>''))->SUM('total_cost');
				$order_list[$key]['total_price'] = $total*$val['discount_rate']+$val['feright']; 
				$order_list[$key]['total_cost'] = $cost; 
				$order_list[$key]['discount_price'] = $total*$val['discount_rate'];
				
				$gross_profit = ($total*$val['discount_rate']+$val['feright'])*$exchange_rate['usd']-$val['actual_feright']-$cost;
				$order_list[$key]['gross_profit'] = $gross_profit;
				$order_list[$key]['gross_profit_margin'] = $gross_profit/(($total*$val['discount_rate']+$val['feright'])*$exchange_rate['usd']);
				
				$arr=explode('.',$order_list[$key]['gross_profit_margin']);
				
				$num1=substr($arr[1],0,2);
				$str = $arr[0].'.'.$num1;
				$order_list[$key]['percentage'] = ($str*100).'%';
				
				
			}
			$data[$k]['salesman'] = $order_list[$k]['salesman'];
			$data[$k]['delivery_time'] = $order_list[$k]['delivery_time'];
			$data[$k]['order_number'] = $order_list[$k]['order_number'];
			$data[$k]['customer_name'] = $order_list[$k]['customer_name'];
			$data[$k]['discount_rate'] = $order_list[$k]['discount_rate'];
			$data[$k]['discount_price'] = $order_list[$k]['discount_price'];
			$data[$k]['feright'] = $order_list[$k]['feright'];
			$data[$k]['total_price'] = $order_list[$k]['total_price'];
			$data[$k]['pay_info'] = $order_list[$k]['pay_info'];
			$data[$k]['actual_feright'] = $order_list[$k]['actual_feright'];
			$data[$k]['tracking_number'] = $order_list[$k]['tracking_number'];
			$data[$k]['total_cost'] = $order_list[$k]['total_cost'];
			
			
			
			$data[$k]['gross_profit'] = $order_list[$k]['gross_profit'];
			$data[$k]['percentage'] = $order_list[$k]['percentage'];
			$data[$k]['remarks'] = $order_list[$k]['remarks'];
			if($order_list[$k]['is_finish']=='1'){
				$data[$k]['is_finish'] = '已审核';
			}else{
				$data[$k]['is_finish'] = '未审核';
			}
			
			
		}
		$filename = "财务管理表";
		$title = array('业务员','发货日期','订单号','客户名称','折扣率','折后货值','收取运费','总金额','收款情况','实际运费','快递单号','采购成本','毛利润','毛利润率','备注','审核状态');
		$res = $this->exportexcel($data,$title,$filename);
	}	
	
	
	
	//导出数据到 excel  ；导出函数
	public function exportexcel($data=array(),$title=array(),$filename='report'){
		header("Content-type:application/octet-stream");
		header("Accept-Ranges:bytes");
		header("Content-type:applicationnd.ms-excel");  
		header("Content-Disposition:attachment;filename=".$filename.".xls");
		header("Pragma: no-cache");
		header("Expires: 0");
		//要添加download  不然下载的就是 txt 格式的文件
		header("Content-Type:application/download");
		//导出xls 开始
		if (!empty($title)){
			foreach ($title as $k => $v) {
				$title[$k]=iconv("UTF-8", "GB2312",$v);
				//$title[$k]=$v;
			}
			$title= implode("\t", $title);
			echo "$title\n";
		}
		if (!empty($data)){
			foreach($data as $key=>$val){
				foreach ($val as $ck => $cv) {
					$data[$key][$ck]=iconv("UTF-8", "GB2312", $cv);
				}
				$data[$key]=implode("\t", $data[$key]);
				
			}
			echo implode("\n",$data);
		}
		
	}
	
	
	//单选进行审核
	public function finish(){
		$id = I('get.id','','htmlspecialchars');
		$time = date('Y-m-d H:i:s');
		$arr = M('order')->field('order_number')->where(array('id'=>$id))->find();
		$arr2 = M('order_info')->where(array('order_number'=>$arr['order_number']))->select();
		foreach($arr2 as $key=>$val){
			$result = M('order_info')->where(array('id'=>$val['id']))->save(array('finish_time'=>$time));
		}
		$res = M('order')->where(array('id'=>$id))->save(array('is_finish'=>'1','finish_time'=>$time));
		
		if($res){
			header("Location:".$_SERVER['HTTP_REFERER']);
		}
	}
	//批量进行审核
	public function finishAll(){
		$id = I('get.id','','htmlspecialchars');
		$ids=I("get.tids",'','htmlspecialchars');
		$id_list = explode(',',trim($ids,','));
		$time = date('Y-m-d H:i:s');
		foreach($id_list as $val){
			$res = M('order')->where(array('id'=>$val))->save(array('is_finish'=>'1','finish_time'=>$time));
			$arr2 = M('order_info')->where(array('id'=>$val))->select();
			foreach($arr2 as $v){
				$result = M('order_info')->where(array('id'=>$v['id']))->save(array('finish_time'=>$time));
			}
		}
		
		header("Location:".$_SERVER['HTTP_REFERER']);
		
	}
	
	//财务编辑
	public function financial_remark(){
		$number 			= I('get.order_number','','htmlspecialchars');
		$info = M('order')->field('order_number,pay_info,actual_feright,tracking_number,remarks')->where(array('order_number'=>$number))->find();

		if(I('post.submit','','htmlspecialchars')){
			$order_number 			= I('post.order_number','','htmlspecialchars');
			$pay_info 			= I('post.pay_info','','htmlspecialchars');
			$actual_feright 	= I('post.actual_feright','','htmlspecialchars');
			$financial_remark 	= I('post.tracking_number','','htmlspecialchars');
			$remarks 			= I('post.remarks','','htmlspecialchars');
			$result = M('order')->where(array('order_number'=>$order_number))->save(array('pay_info'=>$pay_info,'actual_feright'=>$actual_feright,'tracking_number'=>$financial_remark,'remarks'=>$remarks));
			
			if($result){
				header("Location:".$_SERVER['HTTP_REFERER']);
			}
			
		}
		
		$this->assign('info',$info);
		$this->display();
	}
	
	//数据管理
	public function information(){
		//搜索团队下拉框
		$team_list = M('team')->field('id,team_name')->select();
		$members = M('team')->field('id,team_members')->select();
		foreach($members as $val){
			$arr = explode('|',$val['team_members']);
			foreach($arr as $v){
				$members_info .=  $v.',';
			}
		}
		
		//搜索业务员下拉框
		$members_list = array_unique(explode(',',trim($members_info,',')));//唯一性
		
		$pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		
		$res = D('index')->information_show($pageNo,$pageSize);
		
		
/*		$cycle				=	I('get.cycle','','htmlspecialchars');
		echo $cycye;die;
		if($cycle==1){
			$start				=	I('get.start','','htmlspecialchars');
			$end				=	I('get.end','','htmlspecialchars');
		}else if($wheres['cycle']==2){
			$mon_start			=	I('get.mon_start','','htmlspecialchars');
			$mon_end			=	I('get.mon_end','','htmlspecialchars');
		}
			*/
		/*$team				=	I('get.team','','htmlspecialchars');
		
		$team_members		=	I('get.team_members','','htmlspecialchars');*/


		
		if(I('get.search')){	
			$cycle				=	I('get.cycle','','htmlspecialchars');
			$wheres['cycle'] 	= $cycle;
			
			$team				=	I('get.team','','htmlspecialchars');
			if($team!='0'){$wheres	['team'] = $team;}
			$team_members		=	I('get.team_members','','htmlspecialchars');
			if($team_members!='0'){$wheres	['team_members'] = $team_members;}
			
			
			if($wheres['cycle']==1){
				$start				=	I('get.start','','htmlspecialchars');
				$end				=	I('get.end','','htmlspecialchars');
				$wheres['start'] 	= $start;
				$wheres['end'] 		= $end;
			}else if($wheres['cycle']==2){
				$mon_start			=	I('get.mon_start','','htmlspecialchars');
				$mon_end			=	I('get.mon_end','','htmlspecialchars');
				$wheres['mon_start']= $mon_start;
				$wheres['mon_end']  = $mon_end;
				
			}
			
			$index = D("Index");
			$res = $index->information_list($pageNo,$pageSize,$wheres);
			
			
		}
		
		$this->assign("cycle",$cycle);
		$this->assign('start',$start);
		$this->assign("end",$end);
		$this->assign("mon_start",$mon_start);
		$this->assign('mon_end',$mon_end);
		$this->assign('search_team',$team);
		$this->assign('team_members',$team_members);
		
		$count = $res['count'];
		$information_list = $res['information_list'];
		$this->assign("order_number",$search_number);
		$this->assign('salesman',$salesman);
		$this->assign("count",$count);
		$this->assign("customer_name",$customer_name);
		$this->assign('team_list',$team_list);
		$this->assign('information_list',$information_list);
		$this->assign('members_list',$members_list);
		$this->display();
	}
	
	public function team_list(){
		$team = I('post.team','','htmlspecialchars');
		$res = M('team')->where(array('team_name'=>$team))->find();
		$msg = explode('|',$res['team_members']);
		echo json_encode($msg);die;
	}
	
	
	
	public function cycle_info(){
		$msg = I('post.cycle','','htmlspecialchars');
		echo $msg;die;
	}
	
	public function logout() {
		session_unset($_SESSION['user']);
		session_destroy();
//		echo "<script>alert('退出成功!');top.location.href='http://192.168.0.130/index.php/Home/Login/login.html';</script>";
		echo "<script>alert('退出成功!');top.location.href='".U('Login/login')."';</script>";
//		$this->error('对不起,您还没有登录!请先登录!', U('Login/login'), 1);
	}
	
	
}
















